package com.tencent.wxcloudrun.uitls;
import java.util.Date;
import java.util.HashMap;
import cn.hutool.core.date.DateUtil;
import io.jsonwebtoken.*;
/**
 * Created by echisan on 2018/6/23
 */
public class JwtTokenUtils {

    public static final String TOKEN_HEADER = "Authorization";
    public static final String TOKEN_PREFIX = "Bearer ";

    private static final String SECRET = "jwtsecretdemo";
    private static final String ISS = "lichongbing";

    // 角色的key
    private static final String ROLE_CLAIMS = "rol";

    // 过期时间是60秒，既是1分钟
    private static final long EXPIRATION_MINT = 60L;

    // 过期时间是3600秒，既是1个小时
    private static final long EXPIRATION = 3600L;

    // 过期时间是86400秒，既是1整天
    private static final long EXPIRATION_DAY = 86400L;

    // 选择了记住我之后的过期时间为7天
    private static final long EXPIRATION_REMEMBER = 2592000L;


    // api token  30天
    private static final long EXPIRATION_MONTH= 2592000L;



    /**
     * 创建token
     * @param openid
     * @param tableId
     * @param key
     * @param isRememberMe
     * @return
     */
    public static String createToken(String openid,String tableId,String key, boolean isRememberMe) {
        long expiration = isRememberMe ? EXPIRATION_REMEMBER : EXPIRATION;
        HashMap<String, Object> map = new HashMap<>();
        map.put("tableId", tableId);
        map.put("key", key);
        map.put("openid", openid);
        return Jwts.builder()
                .signWith(SignatureAlgorithm.HS512, SECRET)
                .setClaims(map)
                .setIssuer(ISS)
                .setSubject(tableId)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + expiration * 1000))
                .compact();
    }


    /**
     * 创建token
     * @param mobile
     * @param isRememberMe
     * @return
     */
    public static String createTokenBymobile(String mobile, boolean isRememberMe) {
        long expiration = isRememberMe ? EXPIRATION_REMEMBER : EXPIRATION;
        HashMap<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        return Jwts.builder()
                .signWith(SignatureAlgorithm.HS512, SECRET)
                .setClaims(map)
                .setIssuer(ISS)
                .setSubject(mobile)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + expiration * 1000))
                .compact();
    }



    public static String createTokens(String userid,long expiration) {
       // long expiration = EXPIRATION_MINT;

        HashMap<String, Object> map = new HashMap<>();
        map.put("userid", userid);
        return Jwts.builder()
                .signWith(SignatureAlgorithm.HS512, SECRET)
                .setClaims(map)
                .setIssuer(ISS)
                .setSubject(userid)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + expiration * 1000))
                .compact();
    }



    /**
     * 获取用户名
     * @param token
     * @return
     */
    public static String getUsername(String token){
        return getTokenBody(token).getSubject();
    }

    /**
     * 获取手机号
     * @param token
     * @return
     */
    public static String getMobile(String token){
        Object mobile = getTokenBody(token).get("mobile");
        return (String) getTokenBody(token).get("mobile");
    }

    /**
     * 获取userid
     * @param token
     * @return
     */
    public static String getUserid(String token){
        Object userid = getTokenBody(token).get("userid");
        return (String) getTokenBody(token).get("userid");
    }

    //获取tableId
    public static String getTableId(String token){
        Object tableId = getTokenBody(token).get("tableId");
        return (String) getTokenBody(token).get("tableId");
    }
    //获取key
    public static String getKey(String token){
        Object key = getTokenBody(token).get("key");
        return (String) getTokenBody(token).get("key");
    }

    // 获取用户角色
    public static String getUserRole(String token){
        return (String) getTokenBody(token).get(ROLE_CLAIMS);
    }

    // 是否已过期
    public static boolean isExpiration(String token) {
        try {
            return getTokenBody(token).getExpiration().before(new Date());
        } catch (ExpiredJwtException e) {
            return true;
        }
    }



    private static Claims getTokenBody(String token){
        return Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token).getBody();

    }
    public int lengthOfLongestSubstring(String s) {
        int l =0;
        for (int i = 0; i < s.length(); i++) {
            char[] chars = new char[10000];
            int k = 0;
            for (int j = i; j < s.length(); j++) {
                chars[k] = s.charAt(i);
                char tail = s.charAt(j);
                if (s.charAt(i) != s.charAt(j)) {
                    chars[k+1] = s.charAt(j);
                    k = k+1;
                }
            }
            l = k;
        }
        return l;
    }



}
